package com.hq.talk.view.ui;

import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;

import com.hq.lib_comview.common.utils.StringUtils;
import com.hq.lib_comview.view.baseadp.CommonAdapter;
import com.hq.lib_comview.view.baseadp.ViewHolder;
import com.hq.lib_comview.view.scprogress.SVProgressHUD;
import com.hq.lib_comview.view.selview.MyListView;
import com.hq.talk.R;
import com.hq.talk.common.ConstantTag;
import com.hq.talk.model.CompanyModel;
import com.hq.talk.presenter.impl.CompanyPresenter;
import com.hq.talk.presenter.impl.UserPresenter;
import com.hq.talk.view.base.BaseCommAty;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 描述 ：选则公司
 * 作者 ：hq
 * 时间 ：2017/8/9 11:56
 */
public class ChooseCompanyAty extends BaseCommAty implements AdapterView.OnItemClickListener {

    EditText mEditCompany;
    private String mSaveCompany;

    @BindView(R.id.id_company_list_view)
    MyListView mMyListView;
    private CommonAdapter<CompanyModel> mCompanyAdp;
    private List<CompanyModel> mSaveCompanys = new ArrayList<CompanyModel>();

    @Nullable
    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.aty_company, null, false));
    }

    @Override
    public void initPresenter() {
        presenter = new CompanyPresenter();
    }

    @Override
    public void initView() {
        setTitleText(getString(R.string.choose_company), null);
        setLeftText("取消", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        setTitleRight("确定", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (StringUtils.isEmpty(mSaveCompany)) {
                    SVProgressHUD.showInfoWithStatus(aty, "请填写您的公司昵称");
                } else {
                    
                    finish();
                }
            }
        });
        View mView = setFramTitleView(R.layout.layout_search_company);
        mEditCompany = (EditText) mView.findViewById(R.id.id_company_edit);
    }

    @Override
    public void initData() {
        mEditCompany.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mSaveCompany = s + "";
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!StringUtils.isEmpty(mSaveCompany)) {
                    ((CompanyPresenter) presenter).searchCompany(mSaveCompany);
                } else {
                    mSaveCompanys.clear();
                    mCompanyAdp.notifyDataSetChanged();
                }
            }
        });
        mCompanyAdp = new CommonAdapter<CompanyModel>(aty, mSaveCompanys, R.layout.item_company) {
            @Override
            public void convert(ViewHolder helper, CompanyModel item) {
                if (null != item) {
                    helper.setText(R.id.id_company_name_txt, item.companyName);
                }
            }
        };
        mMyListView.setAdapter(mCompanyAdp);
        mMyListView.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        CompanyModel model = (CompanyModel) mCompanyAdp.getItem(position);
        if (null != model) {
            mEditCompany.setText(model.companyName);
            mSaveCompanys.clear();
            mCompanyAdp.notifyDataSetChanged();
        }
    }

    @Override
    public <T> void updateViewWithTag(T t, String tag) {
        if (null != t) {
            if (TextUtils.equals(tag, ConstantTag.Company.GET_COMPANYS)) {
                //获取公司成功
                List<CompanyModel> list = (List<CompanyModel>) t;
                mSaveCompanys.clear();
                if (null != list && !list.isEmpty()) {
                    mSaveCompanys.addAll(list);
                    mCompanyAdp.notifyDataSetChanged();
                } else {
                    dataStatus(3);
                }
            }
        }
    }
}
